package ase.eleitweg.client.gui;

import java.rmi.RemoteException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.sun.xml.internal.bind.v2.model.core.ID;

import ase.eleitweg.common.Group;
import ase.eleitweg.common.User;
import ase.eleitweg.server.UserManager;

public class GruppeBearbeiten_gui {

	private Text beschreibungText;
	private Text gruppennameText;
	protected Shell shell;
	private UserManager gm;
	private ase.eleitweg.common.Group g;
	private Label nranzeigenLabel;

	private void showError(String reason, Exception e) {
		Error_gui eg = new Error_gui(new Shell(), 0, reason);
		e.printStackTrace();
		shell.close();
	}

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public GruppeBearbeiten_gui(UserManager gm, int id) {
		this.gm = gm;
		try {
			this.g = gm.getGroupById(id);
		} catch (RemoteException e) {
			showError("Cannot communicate with server.", e);
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	private void updateGroup() throws RemoteException {

		ase.eleitweg.common.Group g = new ase.eleitweg.common.Group(Integer
				.parseInt(nranzeigenLabel.getText()),
				gruppennameText.getText(), beschreibungText.getText());

		gm.updateGroup(g);

	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell(SWT.MIN);
		shell.setLayout(new FillLayout());
		Rectangle screenSize = Display.getCurrent().getClientArea();

		shell.setMinimumSize(new Point(640, 400));
		shell.setSize(640, 400);
		shell.setLocation((screenSize.width - shell.getSize().x) / 2,
				(screenSize.height - shell.getSize().y) / 2);
		shell.setText("Gruppe bearbeiten");

		final Display display = Display.getCurrent();
		Image icon = new Image(display, "icons/32/agt_web.png");
		shell.setImage(icon);

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new FormLayout());

		final Composite composite_1 = new Composite(composite, SWT.NONE);
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 3;
		composite_1.setLayout(gridLayout_1);
		final FormData fd_composite_1 = new FormData();
		fd_composite_1.bottom = new FormAttachment(0, 160);
		fd_composite_1.right = new FormAttachment(100, -10);
		fd_composite_1.top = new FormAttachment(0, 10);
		fd_composite_1.left = new FormAttachment(0, 10);
		composite_1.setLayoutData(fd_composite_1);

		final Label nrLabel = new Label(composite_1, SWT.NONE);
		nrLabel.setLayoutData(new GridData(150, SWT.DEFAULT));
		nrLabel.setText("Nr.");

		nranzeigenLabel = new Label(composite_1, SWT.BORDER);
		nranzeigenLabel.setLayoutData(new GridData(50, SWT.DEFAULT));
		nranzeigenLabel.setText(Integer.toString(g.getId()));
		new Label(composite_1, SWT.NONE);

		final Label gruppennameLabel = new Label(composite_1, SWT.NONE);
		gruppennameLabel.setText("Gruppenname: *");

		gruppennameText = new Text(composite_1, SWT.BORDER);
		final GridData gd_gruppennameText = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_gruppennameText.widthHint = 300;
		gruppennameText.setLayoutData(gd_gruppennameText);
		gruppennameText.setText(g.getName());
		new Label(composite_1, SWT.NONE);

		final Label beschreibungLabel = new Label(composite_1, SWT.NONE);
		beschreibungLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
				false));
		beschreibungLabel.setText("Beschreibung:");

		beschreibungText = new Text(composite_1, SWT.BORDER);
		final GridData gd_beschreibungText = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_beschreibungText.heightHint = 45;
		beschreibungText.setLayoutData(gd_beschreibungText);
		beschreibungText.setText(g.getDescription());

		final Label pflichteingabeLabel = new Label(composite_1, SWT.NONE);
		pflichteingabeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM,
				true, false));
		pflichteingabeLabel.setText("* = Pflichteingabe");

		Composite composite_2;
		composite_2 = new Composite(composite, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite_2.setLayout(gridLayout);
		final FormData fd_composite_2 = new FormData();
		fd_composite_2.left = new FormAttachment(0, 235);
		fd_composite_2.top = new FormAttachment(0, 315);
		fd_composite_2.right = new FormAttachment(100, -10);
		fd_composite_2.bottom = new FormAttachment(100, -10);
		composite_2.setLayoutData(fd_composite_2);

		final Button speichernButton = new Button(composite_2, SWT.NONE);
		final GridData gd_speichernButton = new GridData(SWT.RIGHT, SWT.BOTTOM,
				true, true);
		gd_speichernButton.widthHint = 80;
		speichernButton.setLayoutData(gd_speichernButton);
		speichernButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(final MouseEvent e) {
				if (!gruppennameText.getText().isEmpty()) {
					try {
						updateGroup();

						shell.close();

					} catch (RemoteException re) {
						showError("Error during communication with server.", re);
					}

				} else {
					MessageBox box = new MessageBox(shell, SWT.ICON_INFORMATION);
					box.setMessage("Ung�ltige Eingabe");
					box.open();
				}

			}
		});
		speichernButton.setText("Speichern");

		final Button zuruecksetzenButton = new Button(composite_2, SWT.NONE);
		zuruecksetzenButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(final MouseEvent e) {
				try {
					gruppennameText.setText(g.getName());
					beschreibungText.setText(g.getDescription());
				} catch (IllegalArgumentException iae) {
					iae.printStackTrace();

				}

			}
		});
		zuruecksetzenButton.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM,
				false, false));
		zuruecksetzenButton.setText("Zur\u00FCcksetzen");

		final Button abbrechenButton = new Button(composite_2, SWT.NONE);
		final GridData gd_abbrechenButton = new GridData(SWT.LEFT, SWT.BOTTOM,
				false, false);
		gd_abbrechenButton.widthHint = 80;
		abbrechenButton.setLayoutData(gd_abbrechenButton);
		abbrechenButton.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				shell.close();
			}
		});
		abbrechenButton.setText("Abbrechen");
		//
	}

}
